package com.yanqu.road.dao.impl.activity.jiuzhou;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.impl.TempDao;

import java.math.BigInteger;
import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class JiuZhouAbilityDaoImpl extends TempDao {

    public Map<Long,BigInteger> getCanSignUserAbilityList(int titleLimit, int patronsCountLimit) {
        String getCanSignUserSql = "SELECT att.userId,att.ability FROM `t_u_user` idList " +
                "LEFT JOIN t_u_history_attribute att " +
                "on att.userId = idList.userId " +
                "WHERE idList.titleId >= ? AND idList.patronsCount >= ?";
        Map<Long,BigInteger> canSignUserAbilityMap = new HashMap<>();
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(getCanSignUserSql);
            int i = 1;
            call.setInt(i++, titleLimit);
            call.setInt(i++, patronsCountLimit);
            rs = call.executeQuery();
            while (rs.next()) {
                long userId = rs.getLong("userId");
                String ability = rs.getString("ability");
                canSignUserAbilityMap.put(userId,new BigInteger(ability));
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }   finally {
            getDb().closeConnection(call, rs);
        }
        return canSignUserAbilityMap;
    }


}
